package org.testile.datasetgenerifier;

import org.dbunit.dataset.AbstractDataSet;
import org.dbunit.dataset.DataSetException;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.ITable;
import org.dbunit.dataset.ITableIterator;
import org.dbunit.dataset.ITableMetaData;

/**
 *
 * @author amra
 */
public class GenerifiedDataSet extends AbstractDataSet {

	private DatasetGenerifier generifier;
	private IDataSet wrapped;

	public GenerifiedDataSet(IDataSet wrapped, DatasetGenerifier generifier) {
		this.wrapped = wrapped;
		this.generifier = generifier;
	}

	@Override
	public ITable getTable(String tableName) throws DataSetException {
		return new GenerifiedTable(wrapped.getTable(tableName), generifier.getTableGenerifier(tableName));
	}

	@Override
	protected ITableIterator createIterator(boolean reversed) throws DataSetException {
		ITableIterator wrappedIterator = reversed ? wrapped.reverseIterator() : wrapped.iterator();

		return new GenerifiedDataSetIterator(wrappedIterator);
	}

	/**
	 *
	 */
	private class GenerifiedDataSetIterator implements ITableIterator {

		private ITableIterator wrappedIterator;

		public GenerifiedDataSetIterator(ITableIterator wrapped) {
			this.wrappedIterator = wrapped;
		}

		public ITable getTable() throws DataSetException {
			ITable table = wrappedIterator.getTable();
			return new GenerifiedTable(table, generifier.getTableGenerifier(table.getTableMetaData().getTableName()));
		}

		public ITableMetaData getTableMetaData() throws DataSetException {
			return wrappedIterator.getTableMetaData();
		}

		public boolean next() throws DataSetException {
			return wrappedIterator.next();
		}
	}
}
